package yannan.voiplock4ble.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import yannan.voiplock4ble.R;
import yannan.voiplock4ble.eventbus.EventObject;
import yannan.voiplock4ble.utils.DeviceUtils;
import yannan.voiplock4ble.utils.PopShowUtils;
import yannan.voiplock4ble.view.MyProgressDialog;
import yannan.voiplock4ble.view.MyTitleView;

/**
 * 说明：
 * 作者：mainTel
 * 时间：2016/8/19 11:04
 * 备注：
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Bundle savedInstanceState;
    protected Toolbar toolbar;
    protected Context mContext;

    protected MyTitleView titleView;

    public enum TitleTheme {
        TOOLBAR_THEME_HOME,
        TOOLBAR_THEME_COMMON;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
        mContext = this;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        }
        Transition slide = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            slide = TransitionInflater.from(this).inflateTransition(R.transition.fade);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setAllowEnterTransitionOverlap(true);
                getWindow().setExitTransition(slide);  //进入动画
                getWindow().setEnterTransition(slide); //退出动画
            }
        }


        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Class<? extends BaseActivity> attr = getClass();
        Content annotation = attr.getAnnotation(Content.class);
        if (annotation != null) {
            int value = annotation.value();
            setContentView(value);
            ButterKnife.bind(this);
        }
        EventBus.getDefault().register(this);
        initData();
    }

    public abstract void initData();

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 设置标题栏
     *
     * @param theme
     */
    public void setTitleTheme(TitleTheme theme, String titleName) {

        titleView = (MyTitleView) findViewById(R.id.toolbar);
//        RelativeLayout rl_title_root = (RelativeLayout) findViewById(R.id.rl_title_root);
//        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) rl_title_root.getLayoutParams();
//        lp.height = DeviceUtils.getTopBarHeight(this);
//        rl_title_root.setLayoutParams(lp);
        RelativeLayout rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        RelativeLayout.LayoutParams lpTitle = (RelativeLayout.LayoutParams) rl_title.getLayoutParams();
        lpTitle.height = DeviceUtils.getNavigationBarHeight(mContext);
        lpTitle.setMargins(0, DeviceUtils.getStatusBarHeight(mContext), 0, 0);
        rl_title.setLayoutParams(lpTitle);
        if (titleName != null) {
            TextView tv_title_name = (TextView) findViewById(R.id.tv_title_name);
            TextView tv_lock_name = (TextView) findViewById(R.id.tv_lock_name);
            tv_lock_name.setVisibility(View.GONE);
            tv_title_name.setText(titleName);
        }
        titleView.setProgress(DeviceUtils.getTopBarHeight(this));
        switch (theme) {
            case TOOLBAR_THEME_HOME:

                break;
            case TOOLBAR_THEME_COMMON:
                RelativeLayout rl_title_root = (RelativeLayout) findViewById(R.id.rl_title_root);
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) rl_title_root.getLayoutParams();
                lp.height = DeviceUtils.getTopBarHeight(this);
                rl_title_root.setLayoutParams(lp);
                LinearLayout title_left = (LinearLayout) findViewById(R.id.title_left);
                title_left.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
//                toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_white_24dp);
//                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        onBackPressed();
//                    }
//                });
                break;
        }


//        toolbar = (Toolbar) findViewById(R.id.toolbar);
//        if (titleName != null) {
//            toolbar.setTitle(titleName);
//        }
//        setSupportActionBar(toolbar);
//        switch (theme) {
//            case TOOLBAR_THEME_HOME:
//                break;
//            case TOOLBAR_THEME_COMMON:
//                toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_white_24dp);
//                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        onBackPressed();
//                    }
//                });
//                break;
//        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void showProgress() {
        MyProgressDialog.showPro(mContext, false);
    }

    public void dimssProgress() {
        MyProgressDialog.dismissDialog();
    }
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void eventbus(String ev) {
//        //是否强制重装
////        if (ev.equals("nowUpdateversion")) {
////            autoInstallApk();
////        } else if (ev.equals("isUpdatenow")) {
////            PopShowUtils.showCommonDialogWithTitle(this,"版本更新", "版本更新", "取消", "确定", true, new PopShowUtils.IDialogCommonListener() {
////                @Override
////                public void onClick(int direction) {
////                    if (direction == PopShowUtils.COMMON_DIALOG_LEFT) {
////
////                    } else if (direction==PopShowUtils.COMMON_DIALOG_RIGHT){
////                        autoInstallApk();
////                    }
////                }
////            });
////        }
//        autoInstallApk();
//    }





    Activity mActivity;
    List<String> permssionList = new ArrayList<>();
    PermissionLister mPemissionLister;


    public void requestRunTimePermssions(Activity mActivity, String[] permssions, PermissionLister pPemissionLister) {
        this.mActivity = mActivity;
        mPemissionLister = pPemissionLister;
        for (int i = 0; i < permssions.length; i++) {
            if (ContextCompat.checkSelfPermission(mActivity, permssions[i]) != PackageManager.PERMISSION_GRANTED) {
                permssionList.add(permssions[i]);
            }
        }
//      没有被允许的权限
        if (!permssionList.isEmpty()) {
            ActivityCompat.requestPermissions(mActivity, permssionList.toArray(new String[permssionList.size()]), 1);
        } else {
            mPemissionLister.grant();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                List<String> denyList = new ArrayList<>();
                for (String lPermission : permissions) {
//                    拒绝了权限
                    if (ContextCompat.checkSelfPermission(mActivity, lPermission) != PackageManager.PERMISSION_GRANTED) {
                        denyList.add(lPermission);
                    }
                }
                if (denyList.isEmpty()) {
                    mPemissionLister.grant();
                } else {
                    mPemissionLister.deny(denyList);
                }
                break;
        }
    }

    public interface PermissionLister {
        void grant();

        void deny(List<String> pStringList);
    }


}
